//
//  PywFastRegisterApi.m
//  PywSdk
//
//  Created by smallwave on 16/9/22.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywFastRegisterApi.h"
#import "PropertyListReformer.h"
#import "PywMBProgressHUD+Pyw.h"

@interface PywFastRegisterApi ()<ETAPIManagerCallBackDelegate>
/**
 *  1:注册时
 *  2:注册，带密码（可改可不改）登录
 */
@property (nonatomic, assign) NSInteger flag;
@end

@implementation PywFastRegisterApi

- (void)requestFastRegisterWithFlag:(NSInteger)flag
                            success:(Successd)success
                               fail:(Failure)fail{
    self.successd = success;
    self.failure = fail;
    self.flag = flag;
    
    AIFFastRegisterManager *manager = [[AIFFastRegisterManager alloc] init];
    manager.delegate = self;
    manager.flag = flag;
    [manager loadData];
}
- (void)requestFastRegisterLoginWithFlag:(NSInteger)flag
                                 account:(NSString *)account
                                password:(NSString *)password
                                 success:(Successd)success
                                    fail:(Failure)fail{
    self.successd = success;
    self.failure = fail;
    self.flag = flag;
    
    AIFFastRegisterManager *manager = [[AIFFastRegisterManager alloc] init];
    manager.delegate = self;
    manager.flag = flag;
    manager.account = account;
    manager.password = password;
    [manager loadData];
}

#pragma mark - ETAPIManagerCallBackDelegate
/**
 *  请求成功
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallAPIDidSuccess:(ETApiBaseManager *)manager{
    PropertyListReformer *reformer = [[PropertyListReformer alloc] init];
    NSDictionary *dict = [manager fetchDataWithReformer:reformer];
    [PywMBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow];
    
    [self fastRegisterWithDict:dict];
}
/**
 *  请求失败
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallApiDidFailed:(ETApiBaseManager *)manager{
    [PywMBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow];
    
    [self fastRegisterFail];
}

/**
 *  注册成功
 *
 *  @param dict <#dict description#>
 */
- (void)fastRegisterWithDict:(NSDictionary *)dict{
    int resultCode = [[dict objectForKey:@"ack"] intValue];
    
    switch (resultCode) {
        case 200:
        {
            NSDictionary *data = [dict objectForKey:@"data"];
            if (self.successd) {
                self.successd(nil,data);
            }
            
            // 注册并登录
            if (self.flag == 2) {
                NSDictionary *data = [dict objectForKey:@"data"];
                [PywPlatformMain sharedPlatformMain].is_verify_show = [[data objectForKey:@"id_verify_show"] boolValue];
            }
        }
            break;
            
        default:
        {
            NSString *msg = [NSString stringWithFormat:@"%@",dict[@"msg"]];
            [PywMBProgressHUD showError:msg toView:[UIApplication sharedApplication].keyWindow];
            if (self.failure) {
                self.failure([self errorWithCode:resultCode]);
            }
        }
            break;
    }
}
/**
 *  注册失败
 */
- (void)fastRegisterFail
{
    if (self.failure) {
        self.failure([self errorWithCode:0]);
    }
}

@end
